#!/usr/bin/env python

from blend.notifications import notify, sound
from blend import constants
from mxit import handles
import gobject
import gettext

gettext.install('blendIT','locale');

class listener:

   sound = None;
   notify = None;

   def __init__(self,blend):
      self.blend = blend;
      config = blend.config;
      if config.notifications['sounds'][0] == True:
         # Enable sound
         gobject.timeout_add(4000,self.startSound,config);
      if config.notifications['text'] == True:
         # Enable notify
         self.notify = notify.notify();

   def startSound(self,config):
      self.sound = sound.Sound(config.notifications['sounds'][1]);
      return False;

   def status_changed(self,status,reason):
      if status == handles.Status.DISCONNECTED:
         # Sound notify:
         if self.sound:
            self.sound.play('client-offline');
      elif status == handles.Status.ACTIVE:
         # Sound notify:
         if self.sound:
            self.sound.play('client-online');
            # Prevents stuff
            self.sound = None;
            gobject.timeout_add(4000,self.startSound,self.blend.config);
      #print "nL sc";
            
   def room_added(self,room,message=None,inviter=None):
      if self.sound:
         self.sound.play('buddy-invite');

      if self.notify:
         p = constants.C.types[handles.BuddyType.ROOM];
         pImg = p.image;
         if inviter is None:
            inviter = _("Unknown");
         self.notify.notify(pImg,'',_("Joined MultiMx %(multimixname)s.\nInvited by %(name)s") % {'multimixname': str(room.name), 'name': str(inviter)});

   def buddy_updated(self,buddy,presence=None,mood=None,name=None,group=None,old_presence=None):
      if presence !=  None:
         oldPres = buddy.old_presence;
         newPres = presence;
         if buddy.presence == handles.Presence.PENDING:
            # New invite
            if self.sound:
               self.sound.play('buddy-invite');
            if self.notify:
               p = constants.C.types[handles.BuddyType.INVITE];
               pImg = p.image;
               self.notify.notify(pImg,'',_('New invitation from %s') % (str(buddy.name)));
            return;

         if oldPres in (handles.Presence.OFFLINE,handles.Presence.PENDING):
            if newPres != handles.Presence.OFFLINE:
               if self.sound:
                  self.sound.play('buddy-online');
               if self.notify:
                  p = constants.C.presences[buddy.presence];
                  pImg = p.image;
                  self.notify.notify(pImg,'',_('%(name)s is now %(status)s') % {'name': buddy.name, 'status': p.text});
         else:
            if newPres == handles.Presence.OFFLINE:
               if self.sound:
                  self.sound.play('buddy-offline');
    
               if self.notify:
                  p = constants.C.presences[presence];
                  pImg = p.image;
                  self.notify.notify(pImg,'',_('%(name)s is now %(status)s') % {'name': buddy.name, 'status': p.text});

   def room_message_received(self,room,message):
      # sound notify
      if self.sound:
         self.sound.play('buddy-message');

   def message_received(self,message):
      # sound notify
      if self.sound:
         self.sound.play('buddy-message');

   def file_pending(self,file,buddy):
      # sound notify
      if self.sound:
         self.sound.play('buddy-message');

   def file_received(self,file,data):
      # sound notify
      if self.sound:
         self.sound.play('buddy-received-file');